package com.soft.jdbc.student;

import com.soft.jdbc.student.dao.StudentDao;
import com.soft.jdbc.student.dao.StudentDaoImpl;
import com.soft.jdbc.student.entity.Student;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/6/6
 */
public class SMS {
    private static final StudentDao studentDao = new StudentDaoImpl();
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        login();
    }

    private static void login() {
        System.out.println("=========== 学生管理系统 ===========");
        System.out.print("请输入用户名：");
        String username = scanner.next();
        System.out.print("请输入密码：");
        String password = scanner.next();

        if (studentDao.validLogin(username, password)) {
            System.out.println("登录成功！");
            showMenu();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    private static void showMenu() {
        while (true) {
            System.out.println("========== 请选择操作键 ==========");
            System.out.println("1、统计学生人数");
            System.out.println("2、查看学生名单");
            System.out.println("3、按学号查询学生姓名");
            System.out.println("4、按姓名查询学生信息");
            System.out.println("5、修改学生出生日期");
            System.out.println("6、删除学生记录");
            System.out.println("0、退出");
            System.out.print("请选择操作：");

            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    countStu();
                    break;
                case 2:
                    showStuMenu();
                    break;
                case 3:
                    showNameById();
                    break;
                case 4:
                    showStuByName();
                    break;
                case 5:
                    updateStuBirthday();
                    break;
                case 6:
                    deleteStu();
                    break;
                case 0:
                    System.out.println("感谢使用，再见！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    private static void countStu() {
        int count = studentDao.countStudents();
        System.out.println("当前共有学生人数：" + count);
    }

    private static void showStuMenu() {
        List<Student> students = studentDao.getAllStudents();
        System.out.println("========== 学生列表 ==========");
        System.out.println("学号\t\t姓名\t\t性别\t\t年级\t\t电话\t\t\t\t邮箱\t\t\t\t出生日期");
        for (Student student : students) {
            System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getGender()+"\t\t"+student.getGrade()+"\t\t"
            +student.getPhone()+"\t"+student.getEmail()+"\t\t"+student.getBirthday());
        }
    }

    private static void showNameById() {
        System.out.print("请输入学生学号：");
        String id = scanner.next();
        Student student = studentDao.getStudentById(id);

        if (student != null) {
            System.out.println("查询成功！");
            System.out.println(student.getId()+"号的学生姓名为："+student.getName()+"，所在年级："+student.getGrade());
        } else {
            System.out.println("未找到该学号的学生！");
        }
    }

    private static void showStuByName() {
        System.out.print("请输入学生姓名：");
        String name = scanner.next();
        List<Student> students = studentDao.getStudentsByName(name);

        if (!students.isEmpty()) {
            System.out.println("========== 查询结果 ==========");
            System.out.println("学号\t\t姓名\t\t性别\t\t年级\t\t电话\t\t\t\t邮箱\t\t\t\t出生日期");
            for (Student student : students) {
                System.out.println(student.getId()+"\t"+student.getName()+"\t"+student.getGender()+"\t\t"+student.getGrade()+"\t\t"
                        +student.getPhone()+"\t"+student.getEmail()+"\t\t"+student.getBirthday());
            }
        } else {
            System.out.println("未找到匹配的学生！");
        }
    }

    private static void updateStuBirthday() {
        System.out.print("请输入学生学号：");
        String id = scanner.next();
        System.out.print("请输入修改后的生日(yyyy-MM-dd)：");
        String newBirthday = scanner.next();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = format.parse(newBirthday);
            if (studentDao.updateStudentBirthday(id, new java.sql.Date(date.getTime()))) {
                System.out.println("修改成功！");
            } else {
                System.out.println("修改失败，请检查学号是否正确！");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    private static void deleteStu() {
        System.out.print("请输入学生学号：");
        String id = scanner.next();

        Student student = studentDao.getStudentById(id);
        if (student == null) {
            System.out.println("未找到该学号的学生！");
            return;
        }

        System.out.println("确认要删除学号为"+student.getId()+"，姓名为"+student.getName()+"的学生信息吗？(Y/N): ");
        String confirm = scanner.next();

        if ("Y".equalsIgnoreCase(confirm)) {
            if (studentDao.deleteStudent(id)) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败！");
            }
        } else {
            System.out.println("已取消删除操作。");
        }
    }
}
